package com.project.smarthome.main.view;


import android.app.Activity;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.project.smarthome.R;
import com.project.smarthome.base.BaseActivity;
import com.project.smarthome.common.layout.DragLayout;
import com.project.smarthome.common.layout.MyRelativeLayout;
import com.project.smarthome.common.view.StatusBarUtil;
import com.project.smarthome.main.MainContract;
import com.project.smarthome.main.model.UserBasicInfo;
import com.project.smarthome.main.presenter.MainPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 管理主页面4个fragment页面
 */
public class MainActivity extends BaseActivity implements MainContract.View, DragLayout.DragListener {

    private static final String PRE_HUAWEIPUSH_REMIND = "pre_huawei_push_remind";
    public static final int REQUEST_HMS_RESOLVE_ERROR = 1000;
    public static final String EXTRA_RESULT = "intent.extra.RESULT";


    private static Activity sMain;
    @BindView(R.id.img_close)
    ImageView mImgClose;
    @BindView(R.id.img_user_icon)
    RoundedImageView mImgUserIcon;
    @BindView(R.id.txt_username)
    TextView mTxtUsername;
    @BindView(R.id.txt_phone)
    TextView mTxtPhone;
    @BindView(R.id.img_sex)
    ImageView mImgSex;
    @BindView(R.id.lin_head)
    LinearLayout mLinHead;
    @BindView(R.id.ll_host)
    LinearLayout mLlHost;
    @BindView(R.id.ll_code)
    LinearLayout mLlCode;
    @BindView(R.id.app_version)
    TextView mAppVersion;
    @BindView(R.id.tv_exit)
    TextView mTvExit;
    @BindView(R.id.fg_main)
    FrameLayout mFgMain;
    @BindView(R.id.lin_main)
    MyRelativeLayout mLinMain;
    @BindView(R.id.drag_main)
    DragLayout mDragMain;
    private MainContract.Presenter mMainPresenter;
    private Unbinder mUnbinder;
    private long mExitTime;

    public static Activity getMainActivity() {
        return sMain;
    }

    @Override
    protected void initVariable() {
        mMainPresenter = new MainPresenter(this);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_main);
        sMain = this;
        mUnbinder = ButterKnife.bind(this);
        mDragMain.setmDragListener(this);
    }

    @Override
    public void setStatusBar() {
        StatusBarUtil.setTransparent(this);
    }

    @Override
    protected void setTitleBarColor() {
    }

    @Override
    protected void loadData() {
        mMainPresenter.init();}

    @Override
    protected void onPause() {
        super.onPause();
    }



    @Override
    public void onOpen() {
    }

    @Override
    public void onClose() {
    }


    @Override
    public void onDrag(float percent) {
    }

    @Override
    public void updateProfile(UserBasicInfo basicInfo) {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
        mMainPresenter.destroy();
        mMainPresenter = null;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            showToast("再按一次退出程序");
            mExitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }
}
